"""
金叉：2，死叉：-2  向上：1  向下：-1
支持重复计算，但是要指定timekey，只能向前重复计算
"""
class cross_bar:
    def __init__(self):
        self.fx=None
        self.timekey=None
        self.his=[]
    def oncalc(self,b,line,timekey=None):
        if line!=None:
            if timekey != None:
                if timekey==self.timekey:
                    self.his.pop()
                    self.fx=self.his[-1]
            if self.fx!=None:
                if self.fx == 1:
                    if b["low"] < line:  # 死叉
                        self.fx = -2
                    else:#否则继续上涨
                        self.fx = 1
                elif self.fx == -2:#死叉顶部
                    if b["low"] <= line and b["high"]>=line:  # 持续死叉
                        self.fx = -2
                    else:#否则下跌
                        self.fx = -1
                elif self.fx==-1:
                    if b["high"] > line:  # 金叉
                        self.fx = 2
                    else:
                        self.fx = -1#否则继续下跌
                elif self.fx==2:#金叉底部
                    if b["low"] <= line and b["high"]>=line:  # 持续金叉
                        self.fx = 2
                    else:#否则继续上涨
                        self.fx = 1
            else:
                if b["low"]>line:
                    self.fx=1
                elif b["high"]<line:
                    self.fx=-1
            if timekey != None:
                self.timekey=timekey
                self.his.append(self.fx)
                while len(self.his)>2:
                    self.his.pop(0)
        return self.fx